package com.wenlong.service.impl;

import com.wenlong.dao.CommodityDao;
import com.wenlong.dao.impl.CommodityDaoImpl;
import com.wenlong.pojo.Commodity;
import com.wenlong.pojo.Page;
import com.wenlong.service.CommodiftyService;

import java.util.List;

/**
 * @author 苏文龙
 * @version 1.0
 */
public class CommodittyServiceImpl implements CommodiftyService {
    private CommodityDao commodityDao = new CommodityDaoImpl();
    @Override
    public int addCommodity(Commodity commodity) {
        return commodityDao.addCommodity(commodity);
    }

    @Override
    public int updateCommodity(Commodity commodity) {
        return commodityDao.updateCommodity(commodity);
    }

    @Override
    public int deleteCommodity(int id) {
        return commodityDao.deleteCommodity(id);
    }

    @Override
    public Commodity queryCommodityById(int id) {
        return commodityDao.queryCommodityById(id);
    }

    @Override
    public List<Commodity> queryAllCommodity() {
        return commodityDao.queryAllCommodity();
    }

    @Override
    public Page<Commodity> page(int pageNo, int pageSize) {
        Page<Commodity> page = new Page<>();
        page.setPageSize(pageSize);
        int pageTotalCount = commodityDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        int pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Commodity> commodities = commodityDao.queryForPageCommodity(begin,pageSize);
        page.setCommodities(commodities);
        return page;
    }

    @Override
    public Page<Commodity> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Commodity> page = new Page<>();
        page.setPageSize(pageSize);
        int pageTotalCount = commodityDao.queryForPageTotalCountByPrice(min,max);
        page.setPageTotalCount(pageTotalCount);
        int pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Commodity> commodities = commodityDao.queryForPageCommodityByPrice(begin,pageSize,min,max);
        page.setCommodities(commodities);
        return page;
    }
}
